﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using EsDemo.Es;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EsDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EsController : ControllerBase
    {
        private readonly IVisitLogRepository _visitLogRepository;

        public EsController(IVisitLogRepository visitLogRepository)
        {
            _visitLogRepository = visitLogRepository;
        }

        [HttpGet]
        public async Task<IActionResult> QueryAsync(int page = 1, int limit = 10)
        {
            var result = await _visitLogRepository.QueryAsync(page, limit);

            return Ok(new
            {
                total = result.Item1,
                items = result.Item2
            });
        }
        [HttpGet,Route("QueryListAsync")]
        public async Task<IActionResult> QueryListAsync(int page = 1, int limit = 10 ,string fildStr="")
        {
            var result = await _visitLogRepository.QueryListAsync(page, limit, fildStr);

            return Ok(new
            {
                total = result.Item1,
                items = result.Item2
            });
        }

        [HttpPost]
        public async Task<IActionResult> InsertAsync([FromBody] VisitLog visitLog)
        {
            await _visitLogRepository.InsertAsync(visitLog);

            return Ok("新增成功");
        }
        [HttpPost,Route("InsertOtherAsync")]
        public async Task<IActionResult> InsertOtherAsync([FromBody] VisitLog visitLog)
        {
            _visitLogRepository.InsertOtherAsync(visitLog);
            await _visitLogRepository.InsertAsync(visitLog);

            return Ok("新增成功");
        }

        [HttpDelete]
        public async Task<IActionResult> DeleteAsync([Required] string id)
        {
            await _visitLogRepository.DeleteAsync(id);

            return Ok("删除成功");
        }

        [HttpPut]
        public async Task<IActionResult> UpdateAsync([FromBody] VisitLog visitLog)
        {
            await _visitLogRepository.UpdateAsync(visitLog);

            return Ok("修改成功");
        }
    }
}
